package cn.kgc.springboot.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;

/**
 * @author mosin
 * date 2021/12/16
 * @version 1.0
 */
@Component
public final class JsonUtil {

    //将对象转字符串
    public static String toString(Object obj){

        ObjectMapper objectMapper = new ObjectMapper();
        String s = null;
        try {
            s = objectMapper.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return s;
    }

    // json字符串 转对象
    public static Object toObject(String objStr){
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Object o = objectMapper.readValue(objStr, Object.class);
            return  o;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return  null;

    }
}
